package com.tbit.uqbike.service.business.impl;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.StrUtil;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.tbit.uqbike.object.pojo.Account;
import com.tbit.uqbike.object.pojo.dto.CardUsageDTO;
import com.tbit.uqbike.object.pojo.vo.CardUsageSummaryVO;
import com.tbit.uqbike.object.pojo.vo.CardUsageVO;
import com.tbit.uqbike.object.pojo.vo.RideLogDetailsVO;
import com.tbit.common.entity.view.PageResult;
import com.tbit.common.entity.view.Result;
import com.tbit.uqbike.service.business.CardUsageService;
import com.tbit.uqbike.webmanager.dao.capital.CardUseLogDao;
import com.tbit.uqbike.webmanager.dao.capital.RideLogDetailsDao;
import com.tbit.uqbike.webmanager.dao.core.AccountDao;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * 卡券使用记录
 * @author <a href="mailto:liu.haoxiang@tbit.com.cn">BUGXiang</a>
 * @version 1.0
 * @since 2025-2-12
 */
@Service
public class CardUsageServiceImpl implements CardUsageService {

    @Resource
    private CardUseLogDao cardUsageDao;
    @Resource
    private AccountDao accountDao;
    @Resource
    private RideLogDetailsDao rideLogDetailsDao;


    /**
     * 获取卡券使用记录
     * @param dto dto
     * @return Result
     */
    @Override
    public Result<Object> getCardUsage(CardUsageDTO dto) {
        dto.setData();
        if (Objects.isNull(dto.getStartTime()) || Objects.isNull(dto.getEndTime())){
            return Result.error("时间不能为空");
        }
        // 检查时间范围是否超过6个月
        if (DateUtil.betweenMonth(dto.getStartTime(), dto.getEndTime(), true) > 6) {
            return Result.error("时间范围不能超过6个月");
        }
        Map<Integer, String> accouontMap = accountDao.selectByIdAndType(dto.getBrandId(), 1).stream().collect(Collectors.toMap(Account::getAccountId, Account::getName));
        if (Objects.nonNull(dto.getPage()) && Objects.nonNull(dto.getSize())){
            //分页
            PageInfo<CardUsageVO> info = PageHelper.startPage(dto.getPage(), dto.getSize()).doSelectPageInfo(() -> cardUsageDao.getCardUsage(dto));
            checkSend(info.getList(), accouontMap);
            return Result.success(new PageResult<>(dto.getPage(), dto.getSize(), info.getTotal(), info.getPages(), info.getList()));
        }else{
            //不分页
            List<CardUsageVO> cardUsage = cardUsageDao.getCardUsage(dto);
            checkSend(cardUsage, accouontMap);
            return Result.success(cardUsage);
        }
    }


    /**
     * 卡券使用详情
     * @param orderNo 骑行订单号
     * @return Result
     */
    @Override
    public Result<CardUsageVO> getCardUsageDetails(String orderNo) {
        if (StrUtil.isEmpty(orderNo)){
            return Result.error("订单号不能为空");
        }
        RideLogDetailsVO rideLogDetailsVO = rideLogDetailsDao.queryByOrderId(orderNo);
        if (Objects.isNull(rideLogDetailsVO)){
            return Result.error("订单不存在");
        }
        CardUsageVO cardUsageVO = new CardUsageVO();
        cardUsageVO.setUserCode(rideLogDetailsVO.getUserCode())
                .setPhone(rideLogDetailsVO.getUserPhone())
                .setRideMoney(rideLogDetailsVO.getCyclingMoney())
                .setCardDeductionMoney(rideLogDetailsVO.getCardDeductionMoney());
        return Result.success(cardUsageVO);
    }


    /**
     * 获取卡券使用记录统计
     * @param dto dto
     * @return Result
     */
    @Override
    public Result<CardUsageSummaryVO> getCardUsageSummary(CardUsageDTO dto) {
        dto.setData();
        if (Objects.isNull(dto.getStartTime()) || Objects.isNull(dto.getEndTime())){
            return Result.error("时间不能为空");
        }
        // 检查时间范围是否超过6个月
        if (DateUtil.betweenMonth(dto.getStartTime(), dto.getEndTime(), true) > 6) {
            return Result.error("时间范围不能超过6个月");
        }
        return Result.success(cardUsageDao.groupByCardType(dto));
    }


    /**
     * 处理数据
     * @param list List
     * @param accountMap Map
     */
    private void checkSend(List<CardUsageVO> list, Map<Integer, String> accountMap){
        for (CardUsageVO cardUsageVO : list) {
            if (Objects.isNull(cardUsageVO.getMoney())){
                cardUsageVO.setRemark("赠送");
            }
            if (Objects.nonNull(cardUsageVO.getBuyAccountId())){
                cardUsageVO.setBuyAccountName(accountMap.get(cardUsageVO.getBuyAccountId()));
                cardUsageVO.setUseAccountName(accountMap.get(cardUsageVO.getUseAccountId()));
            }
        }
    }

}
